#include "stdafx.h"
#include "GoodsManager.h"
#include "XDB.h"

GoodsManager::GoodsManager() {
}


GoodsManager ::~GoodsManager() {
    reset();
}

GoodsPtr GoodsManager::getGoods(uint32_t code, bool createNew) {
    auto iterFind = m_goods.find(code);
    if (iterFind != m_goods.end()) {
        return iterFind->second;
    }
    
    if (createNew) {
        GoodsPtr pGoods = std::make_shared<CGoods>();
        m_goods[code] = pGoods;
        pGoods->m_code = code;
        
        return pGoods;
    }
    return nullptr;
}


GoodsPtr GoodsManager::createGoods(uint32_t code) {
    GoodsPtr pGoods = std::make_shared<CGoods>();
    m_goods[code] = pGoods;
    pGoods->m_code = code;
    return pGoods;
}

void GoodsManager::getGoodsList(std::vector<GoodsPtr> &goodsList, int exchange, int64_t categroy) {

    for (auto kv : m_goods) {
        goodsList.push_back(kv.second);
    }
}

void GoodsManager::init() {

    /*google::protobuf::RepeatedPtrField<XCode> codes;
    xDB.getAllCodes(codes);
    
    for (size_t i = 0; i < codes.size(); i++) {
        auto code = codes[i].stock_code();
        auto pGoods = getGoods(code, true);
    
        pGoods->m_codeText = SOUI::SStrCpCvt::FromUTF8(codes[i].code_name().c_str());
        pGoods->m_name = SOUI::SStrCpCvt::FromUTF8(codes[i].stock_name().c_str());
        pGoods->trade_session = codes[i].trade_session();
        pGoods->exchange = codes[i].exchange();
        pGoods->category = codes[i].category();
    }*/
}

void GoodsManager::reset() {
    m_goods.clear();
}

GoodsManager &getGoodsManager() {
    static GoodsManager g;
    return g;
}
